package com.andon.microcellsimulator.common.base;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.os.Process;
import android.telephony.TelephonyManager;

import com.andon.microcellsimulator.activity.MainActivity;
import com.andon.microcellsimulator.common.dialog.ZnkAlertDialog;
import com.andon.microcellsimulator.common.util.ZnkActivityUtil;
import com.andon.microcellsimulator.http.IOTClient;
import com.andon.microcellsimulator.model.GlobalBeanManager;
import com.tencent.bugly.crashreport.CrashReport;

import java.util.List;

public class ZnkApplication extends Application {

    private static final String BUGLY_APP_ID = "4d604b3aef";

    public static float DENSITY;

    public static SharedPreferences APP_SP;

    private static AppHandler sHandler = null;

    private String appID;

//    CrashHandler crashHandler;

//    private static ZnkDBHelper dbHelper;

    @Override
    public void onCreate() {
        super.onCreate();

        DENSITY = getResources().getDisplayMetrics().density;
        APP_SP = getSharedPreferences("data", MODE_PRIVATE);

        TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
        appID = TelephonyMgr.getDeviceId();

//        // 设置全局崩溃异常处理
//        crashHandler = CrashHandler.getInstance();
//        crashHandler.init(getApplicationContext());

        /* Bugly SDK初始化
        * 参数1：上下文对象
        * 参数2：APPID，平台注册时得到,注意替换成你的appId
        * 参数3：是否开启调试模式，调试模式下会输出'CrashReport'tag的日志
        */
        CrashReport.initCrashReport(getApplicationContext(), BUGLY_APP_ID, true);

        if (sHandler == null) {
            sHandler = new AppHandler(getApplicationContext());
        }

        IOTClient.getInstance().initOkGo(this);

    }

    public String getAppID() {
        return appID;
    }

    public static AppHandler getHandler() {
        return sHandler;
    }

    public static class AppHandler extends Handler {

        private Context context;

        public AppHandler(Context context) {
            this.context = context;
        }

        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case 500:
                    // 令牌失效或过期
                    backToLogin(context, msg.obj.toString());
            }
        }
    }

//    public synchronized ZnkDBHelper getDBHelper() {
//        if (dbHelper == null) {
//            dbHelper = new ZnkDBHelper(this);
//        }
//
//        go_back_white dbHelper;
//    }

    private boolean shouldInit() {
        ActivityManager am = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE));
        List<ActivityManager.RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();
        String mainProcessName = getPackageName();
        int myPid = Process.myPid();
        for (ActivityManager.RunningAppProcessInfo info : processInfos) {
            if (info.pid == myPid && mainProcessName.equals(info.processName)) {
                return true;
            }
        }
        return false;
    }

    private static void backToLogin(Context context, String content) {

        GlobalBeanManager.setGlobalBean(null);

        ZnkActivityUtil.showTokenErrorDialog(content, new ZnkAlertDialog.OnClickCallBack() {
            @Override
            public void exec() {
                ZnkActivityUtil.startActivity(ZnkActivityUtil.getContext(), MainActivity.class);
            }
        });

    }

}
